package com.sec.etech.employeesync.confighandler;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sec.etech.employeesync.entity.OrgEmployeeZTertiaryLevelJob;
import com.sec.etech.employeesync.mapper.OrgEmployeeZTertiaryLevelJobMapper;
import com.sec.etech.employeesync.util.ConfigTableHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Slf4j
public class TertiaryLevelJobConfigHandler implements ConfigTableHandler<OrgEmployeeZTertiaryLevelJob> {

    @Resource
    private OrgEmployeeZTertiaryLevelJobMapper tertiaryLevelJobMapper;

    @Override
    public Long getOrCreateConfigId(String... params) {
        String code = params[0];
        String name = params[1];
        OrgEmployeeZTertiaryLevelJob tertiaryLevelJob = getEntityByName(code, name);
        if (tertiaryLevelJob == null) {
            tertiaryLevelJob = new OrgEmployeeZTertiaryLevelJob();
            tertiaryLevelJob.setCode(code);
            tertiaryLevelJob.setName(name);
            tertiaryLevelJob.setActive(1);
            insertEntity(tertiaryLevelJob);
            log.info("新增三级岗位配置:name={}", name);
        }
        return tertiaryLevelJob.getTertiaryLevelJobId();
    }

    @Override
    public OrgEmployeeZTertiaryLevelJob getEntityByName(String name) {
        return tertiaryLevelJobMapper.selectOne(Wrappers.<OrgEmployeeZTertiaryLevelJob>lambdaQuery()
                .eq(OrgEmployeeZTertiaryLevelJob::getName, name));
    }

    public OrgEmployeeZTertiaryLevelJob getEntityByName(String code, String name) {
        return tertiaryLevelJobMapper.selectOne(Wrappers.<OrgEmployeeZTertiaryLevelJob>lambdaQuery()
                .eq(OrgEmployeeZTertiaryLevelJob::getCode, code)
                .eq(OrgEmployeeZTertiaryLevelJob::getName, name));
    }

    @Override
    public String generateNextCode() {
        return null;
    }

    @Override
    public void insertEntity(OrgEmployeeZTertiaryLevelJob entity) {
        tertiaryLevelJobMapper.insert(entity);
    }
}
